package com.hmsm.dingtalk;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dingtalk.open.client.api.model.corp.JsapiTicket;
import com.dingtalk.open.client.api.model.corp.SnsPersistentCode;
import com.dingtalk.open.client.api.model.corp.SnsToken;
import com.dingtalk.open.client.api.model.corp.SnsUserInfoResult;
import com.dingtalk.open.client.api.service.corp.JsapiService;
import com.dingtalk.open.client.api.service.corp.SnsService;
import com.dingtalk.open.client.common.ParamAttr;
import com.dingtalk.open.client.common.ServiceException;
import com.hmsm.dingtalk.openapi.EnterpriseConfig;
import com.hmsm.server.util.URLHelp;

public class HmsmSnsService implements SnsService {
	
	private static final Log log = LogFactory.getLog(HmsmSnsService.class);
	
	public long cacheTime = 1000 * 60 * 55 * 2;

	@Override
	public String getToken(String appId, String appSecret) throws ServiceException {
		String ticketValue = null;
		
		URLHelp help = new URLHelp();

		String result;
		try {
			result = help.requestRul(EnterpriseConfig.OAPI_HOST+"/sns/gettoken?appid="+appId+"&appsecret="+appSecret, "GET", "UTF-8");
		} catch (Exception e) {
			result = null;
			log.warn("访问失败："+e);
		}
		if (result!=null){
			JSONObject jsonResult = JSONObject.fromObject(result);
			if (jsonResult.containsKey("errcode")){
				if (jsonResult.getInt("errcode")==0){
					ticketValue = jsonResult.getString("access_token");
				}else{
					log.warn("调用失败："+ jsonResult.getString("errmsg"));
				}
			}
		}
		
		return ticketValue;
	}

	@Override
	public SnsPersistentCode getPersistentCode(String appToken, String tmpAuthCode) throws ServiceException {
		SnsPersistentCode persistentCode = null;
		
		URLHelp help = new URLHelp();

		String data = "{\"tmp_auth_code\":\""+tmpAuthCode+"\"}";
		String result;
		try {
			result = help.postJson(EnterpriseConfig.OAPI_HOST+"/sns/get_persistent_code?access_token="+appToken, data, "UTF-8");
		} catch (Exception e) {
			result = null;
			log.warn("访问失败："+e);
		}
		if (result!=null){
			JSONObject jsonResult = JSONObject.fromObject(result);
			if (jsonResult.containsKey("errcode")){
				if (jsonResult.getInt("errcode")==0){
					persistentCode = new SnsPersistentCode();
					persistentCode.setOpenid(jsonResult.getString("openid"));
					persistentCode.setPersistent_code(jsonResult.getString("persistent_code"));
					persistentCode.setUnionid(jsonResult.getString("unionid"));
				}else{
					log.warn("调用失败："+ jsonResult.getString("errmsg"));
				}
			}
		}
		
		return persistentCode;
	}

	@Override
	public SnsToken getSnsToken(String appToken, String openid, String persistentCode) throws ServiceException {
		SnsToken snsToken = null;
		
		URLHelp help = new URLHelp();

		String data = "{\"openid\":\""+openid+"\",\"persistent_code\":\""+persistentCode+"\"}";
		String result;
		try {
			result = help.postJson(EnterpriseConfig.OAPI_HOST+"/sns/get_sns_token?access_token="+appToken, data, "UTF-8");
		} catch (Exception e) {
			result = null;
			log.warn("访问失败："+e);
		}
		if (result!=null){
			JSONObject jsonResult = JSONObject.fromObject(result);
			if (jsonResult.containsKey("errcode")){
				if (jsonResult.getInt("errcode")==0){
					snsToken = new SnsToken();
					snsToken.setSns_token(jsonResult.getString("sns_token"));
					snsToken.setExpires_in(jsonResult.getLong("expires_in"));
				}else{
					log.warn("调用失败："+ jsonResult.getString("errmsg"));
				}
			}
		}
		
		return snsToken;
	}

	@Override
	public SnsUserInfoResult getUserInfo(String paramString) throws ServiceException {
		// TODO Auto-generated method stub
		return null;
	}

}
